package com.situ.personal.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.situ.personal.dao.IUserDao;
import com.situ.personal.pojo.User;
import com.situ.personal.service.IUserService;
import com.situ.personal.service.impl.UserServiceImpl;
import com.situ.personal.util.JDBCUtil;
import com.situ.personal.util.JSONResult;
import com.situ.personal.util.JSONUtil;
import com.situ.personal.util.LayUITableResult;

// SSM:SpringMVC+Spring+MyBatis
// http://localhost:8080/Personal/user
// 127.0.0.1:8080/Personal/user
@WebServlet("/user")
public class UserServlet extends HttpServlet{
	private IUserService userService = new UserServiceImpl();
	
	// Ctrl+D：删除光标所在的行
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// syst
		// System.out.println("UserServlet.service()");
		// http://localhost:8080/Personal/user?method=selectAll
		// http://localhost:8080/Personal/user?method=deleteById&id=2
		String method = req.getParameter("method");
		if (method == null || "".equals(method)) {
			method = "selectAll";
		}
		switch (method) {
		case "selectByPage":
			selectByPage(req, resp);
			break;
		case "selectAll":
			selectAll(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		case "deleteAll":
			deleteAll(req, resp);
			break;
		case "add":
			add(req, resp);
			break;
		case "getUserUpdatePage":
			getUserUpdatePage(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		case "login":
			login(req, resp);
			break;
		case "logout":
			logout(req, resp);
			break;
		default:
			break;
		}
		
	}

	private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		HttpSession session = req.getSession();
		session.invalidate();
		resp.sendRedirect(req.getContextPath() + "/login.jsp");
	}

	private void login(HttpServletRequest req, HttpServletResponse resp) {
		String name = req.getParameter("name");
		String password = req.getParameter("password");
		String code = req.getParameter("code");
		
		HttpSession session = req.getSession();
		String codeInSession = (String) session.getAttribute("codeInSession");
		if (code == null || "".equals(code) || !codeInSession.equalsIgnoreCase(code)) {
			JSONUtil.obj2Json(JSONResult.error("验证码为空或错误"), resp);
			return;
		}
		
		User user = userService.login(name, password);
		if (user != null) {
			System.out.println(user);
			session.setAttribute("user", user);
			JSONUtil.obj2Json(JSONResult.ok("登录成功"), resp);
		} else {
			JSONUtil.obj2Json(JSONResult.error("用户名密码错误"), resp);
		}
		
	}

	private void update(HttpServletRequest req, HttpServletResponse resp) {
		int id  = Integer.parseInt(req.getParameter("id"));
		String name = req.getParameter("name");
		int age = Integer.parseInt(req.getParameter("age"));
		String gender = req.getParameter("gender");
		String password = req.getParameter("password");
		Integer type = Integer.parseInt(req.getParameter("type"));
		User user = new User(id, name, age, gender, password, type);
		userService.update(user);
		
		JSONUtil.obj2Json(JSONResult.ok("修改成功"), resp);
	}

	private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
		// "34, 45"
		String ids = req.getParameter("ids");
		String[] array = ids.split(",");
		userService.deleteAll(array);
		
		JSONUtil.obj2Json(JSONResult.ok("删除成功"), resp);
	}

	private void getUserUpdatePage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Integer id = Integer.parseInt(req.getParameter("id"));
		User user = userService.selectById(id);
		req.setAttribute("user", user);
		req.getRequestDispatcher("/user_update.jsp").forward(req, resp);
	}

	// /user/selectByPage?page=1&limit=10
	private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("UserServlet.selectByPage()");
		int page = Integer.parseInt(req.getParameter("page"));
		Integer limit = Integer.parseInt(req.getParameter("limit"));
		LayUITableResult layUITableResult = userService.selectByPage(page, limit);
		System.out.println(layUITableResult);
		
		JSONUtil.obj2Json(layUITableResult, resp);
	}

	private void add(HttpServletRequest req, HttpServletResponse resp) {
		String name = req.getParameter("name");
		Integer age = Integer.parseInt(req.getParameter("age"));
		String gender = req.getParameter("gender");
		String password = req.getParameter("password");
		Integer type = Integer.parseInt(req.getParameter("type"));
		User user = new User(name, age, gender, password, type);
		userService.add(user);
		
		JSONUtil.obj2Json(JSONResult.ok("添加成功"), resp);
	}

	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		userService.deleteById(Integer.parseInt(id));
		// 删除完了之后，重定向到查找所有界面
		// /Personal/user
		// resp.sendRedirect(req.getContextPath() + "/user?method=selectAll");
		
		// JSONResult jsonResult = new JSONResult(JSONResult.OK, "删除成功");
		// JSONUtil.array2Json(jsonResult, resp);
		JSONUtil.obj2Json(JSONResult.ok("删除成功"), resp);
	}

	private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<User> list = userService.selectAll();
		// 转发
		// 把list数据放到req内存里面
		req.setAttribute("list", list);
		// 转发到user_list.jsp展示数据
		req.getRequestDispatcher("/user_list.jsp").forward(req, resp);
	}

}
